package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.file.system.plugin.shiro.ShiroTools;
import com.file.system.tools.page.DP;
import com.file.system.entity.res.DataPage;
import cn.hutool.core.util.IdUtil;
import static com.file.system.tools.Formater.getTime;
import cn.hutool.core.util.ObjectUtil;
import java.time.LocalDateTime;
<#if table.fieldNames?contains("sort")>
import com.file.system.entity.sort.BaseSort;
import com.file.system.tools.sort.SortUtil;
</#if>


/**
* ${cfg.controllerName} 实现
* @author ${author}
* @since ${date}
*/
@Service
@Transactional(rollbackFor = Exception.class)
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} implements ${table.serviceName} {

    @Autowired
    private ${table.mapperName} mapper;

<#if table.fieldNames?contains("sort")>
    @Autowired
    private SortUtil sortUtil;
</#if>


    //添加${cfg.controllerName}
    public boolean save(${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}.setId(IdUtil.getSnowflakeNextId());
    <#if table.fieldNames?contains("sort")>
        //排序
        ${entity?uncap_first} = sortUtil.add(${entity?uncap_first}, ${entity?uncap_first}.getClass(), "${table.name}",
            BaseSort.eq().val("is_del", "0")
        );
        if (ObjectUtil.isNull(${entity?uncap_first})) {
            return false;
        }
    </#if>
        return SqlHelper.retBool(mapper.save(${entity?uncap_first}));
    }

<#list table.fields as field>
<#if field_index == 0 && field.customMap.engine != "MRG_MYISAM">
    //添加${cfg.controllerName}(批量)
    public boolean saveBatch(@Param("list") List<${entity}> ${entity?uncap_first}List) {
        if(${entity?uncap_first}List.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(${entity?uncap_first}List));
    }
</#if>
</#list>

    //添加或更新${cfg.controllerName}
    public boolean saveOrUpdate(${entity} ${entity?uncap_first}) {
        return ObjectUtil.isNull(${entity?uncap_first}.getId()) ? save(${entity?uncap_first}) : updateById(${entity?uncap_first});
    }


    //编辑${cfg.controllerName}
    public boolean updateById(@Param("et") ${entity} ${entity?uncap_first}) {
        if (ObjectUtil.isEmpty(${entity?uncap_first}.getId())) {
            return false;
        }
    <#if table.fieldNames?contains("sort")>
        //查询
        ${entity} ${entity?uncap_first}Old = mapper.selectById(${entity?uncap_first}.getId());
        //排序
        ${entity?uncap_first} = sortUtil.update(${entity?uncap_first}, ${entity?uncap_first}.getClass(), ${entity?uncap_first}Old.getSort(), "${table.name}",
            BaseSort.eq().val("is_del", "0")
        );
        if (ObjectUtil.isNull(${entity?uncap_first})) {
            return false;
        }
    </#if>
        return SqlHelper.retBool(mapper.updateById(${entity?uncap_first}));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<${entity}> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //删除${cfg.controllerName}
    public boolean removeById(@Param("id") Long id) {
    <#if table.fieldNames?contains("sort")>
        //排序
        ${entity} ${entity?uncap_first} = mapper.selectById(id);
        sortUtil.del(${entity?uncap_first}, ${entity?uncap_first}.getClass(), "${table.name}",
            BaseSort.eq().val("is_del", "0")
        );
    </#if>
        return SqlHelper.retBool(mapper.removeById(new ${entity}().setId(id)));
    }

    //通过ids删除${cfg.controllerName}
    public boolean removeByIds(List<Long> ids) {
        UpdateWrapper<${entity}> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_del", 1);
        updateWrapper.set("delete_by", ShiroTools.getId());
        updateWrapper.set("delete_name", ShiroTools.getName());
        updateWrapper.set("delete_time", LocalDateTime.now());
        updateWrapper.in("id", ids);

        int state = mapper.update(updateWrapper);
        if (state < 1) {
            return false;
        }
        <#if table.fieldNames?contains("sort")>
        //重新排序
        List<${entity}> ${entity?uncap_first}s = listByIds(ids);
        for (${entity} ${entity?uncap_first} : ${entity?uncap_first}s) {
            sortUtil.del(${entity?uncap_first}, ${entity?uncap_first}.getClass(), "${table.name}",
                BaseSort.eq().val("is_del", 0)
            );
        }
        </#if>
        return true;
    }

    //查询${cfg.controllerName}
    @DS("slave")
    public List<${entity}> list(@Param("ew") QueryWrapper<${entity}> query) {
        return mapper.select(query);
    }

    //通过ids查询${cfg.controllerName}集合
    @DS("slave")
    public List<${entity}> listByIds(List<Long> ids) {
        QueryWrapper<${entity}> query = new QueryWrapper<>();
        query.in("a.id", ids);
    <#if table.fieldNames?contains("sort")>
        query.orderByAsc("a.sort");
    <#else>
        query.orderByDesc("a.id");
    </#if>
        return mapper.select(query);
    }

    //查询所有非删除状态${cfg.controllerName}
    @DS("slave")
    public List<${entity}> listAll() {
        QueryWrapper<${entity}> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
    <#if table.fieldNames?contains("sort")>
        query.orderByAsc("a.sort");
    <#else>
        query.orderByDesc("a.id");
    </#if>
        return mapper.select(query);
    }

    //查询${cfg.controllerName}分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize,  @Param("ew") QueryWrapper<${entity}> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<${entity}> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<${entity}> list = mapper.select(page, query);
        return DataPage.init().set(count,list);
    }

    //通过id查询${cfg.controllerName}
    @DS("slave")
    public ${entity} getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询${cfg.controllerName}
    @DS("slave")
    public ${entity} getByOne(@Param("ew") QueryWrapper<${entity}> query) {
        List<${entity}> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询${cfg.controllerName}数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<${entity}> query) {
        return mapper.selectCount(query);
    }
}
</#if>
